package com.orderServlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.order.OrderDao;
import com.vo.Order;

public class OrderViewServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html,utf-8");
		int page = 0;
		int pageCount = 10;
		int count = 0;
		OrderDao dao = new OrderDao();
		List<Order> orderList= new ArrayList<Order>();
		if(request.getParameter("page") == null || Integer.parseInt(request.getParameter("page"))< 1){
			page =1;
		}else{
			page = Integer.parseInt(request.getParameter("page"));
		}
		try {
			if( dao.getCount()%pageCount >= 1){
			
				count = dao.getCount()/pageCount+1;
			}else{
				count = dao.getCount()/pageCount;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(request.getParameter("submit") == null){
			
			String s = request.getParameter("submit");
			
			try {
				orderList = dao.getAll(page,pageCount);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			session.setAttribute("AllOrder", orderList);
		}else{
			//传入两个值 ID以及下单用户
			
			int  orderId =0;
			String orderUser = null;
			List<Order> oList= new ArrayList<Order>();
			if(request.getParameter("orderId") != null || !request.getParameter("orderId").equals("")|| request.getParameter("userName") != null || !request.getParameter("userName").equals("")){
				try {
					orderId = Integer.parseInt(request.getParameter("orderId"));
				} catch (Exception e) {
					// TODO: handle exception
				}
				orderUser = request.getParameter("userName");
				
				try {
					orderList = dao.getAll(page,pageCount);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				for(int a=0;a<orderList.size();a++){
					if(orderId == orderList.get(a).getEo_id() && orderUser.equals(orderList.get(a).getEo_user_name())){
						Order order = new Order();
						order = orderList.get(a);
						
						oList.add(order);
						System.out.println(order.getEo_user_name());
						break;
						
					}else{
						try {
							oList = dao.getAll(page,pageCount);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println("11error");
					}
				}
			}
			session.setAttribute("AllOrder", oList);
		}
		
		if(page == count){
			session.setAttribute("page", page-1);
		}else{
			session.setAttribute("page", page);
		}
		session.setAttribute("count", count);
		
		request.getRequestDispatcher("manage/order.jsp").forward(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
